import Router from 'koa-router'
import axios from './utils/axios'
import Province from '../dbs/models/province'



let router = new Router({
  prefix: '/geo'
})

const sign = '2887e552643c295f7301096e12134e8d';

// 获取城市定位
router.get('/getPosition', async (ctx) => {
  let {
    status,
    data: {
      province,
      city
    }
  } = await axios.get(`http://cp-tools.cn/geo/getPosition?sign=${sign}`)
  if (status === 200) {
    ctx.body = {
      province,
      city
    }
  } else {
    ctx.body = {
      province: '',
      city: ''
    }
  }
})

// 获取首页菜单
router.get('/menu', async (ctx) => {
  let {
    status,
    data: {
      menu
    }
  } = await axios.get(`http://cp-tools.cn/geo/menu?sign=${sign}`)
  if (status === 200) {
    ctx.body = {
      menu
    }
  } else {
    ctx.body = {
      menu: ''
    }
  }
})

router.get('/province', async (ctx) => {
  // 这是走的数据库的数据
  // let province = await Province.find()
  // ctx.body = {
  //   province: province.map(item => {
  //     return {
  //       id: item.id,
  //       name: item.value[0]
  //     }
  //   })
  // }

  // 这是线上数据
  let {
    status,
    data: {
      province
    }
  } = await axios.get(`http://cp-tools.cn/geo/province?sign=${sign}`)
  ctx.body = {
    province: status === 200 ? province : []
  }

})

router.get('/province/:id', async (ctx) => {
  // 在线api
  let {
    status,
    dta: {
      city
    }
  } = await axios.get(`http://cp-tools.cn/geo/province/${ctx.params.id}?sign=${sign}`)
  city.body = {
    city: status === 200 ? city : []
  }
})


router.get('city', async (ctx) => {
  // 在线api
  let {
    status,
    data: {
      city
    }
  } = await axios.get(`http://cp-tools.cn/geo/city?sign=${sign}`)
  ctx.body = {
    city: status === 200 ? city : []
  }
})

router.get('/hotCity', async (ctx) => {
  // 在线api
  let {
    status,
    data: {
      hots
    }
  } = await axios.get(`http://cp-tools.cn/geo/hotCity?sign=${sign}`)
  ctx.body = {
    hots: status === 200 ? hots : []
  }
})



export default router;
